import React, {Component} from 'react';
import {Card,Icon, Table} from 'antd';
import {reqCompanyInformation} from '../../api/index';
import Button from "antd/lib/button";
/*
* 公司信息路由*/
export default class ComInformation extends Component {

    state = {
        companyInformation: [],//总的公司信息列表
    }

    //异步获取公司基本信息

    getCompanyInformation = async () => {

        //发异步ajax请求，获取数据
        const result = await reqCompanyInformation()
        const companyInformation = result.data.results
        //更新状态
        this.setState({
            companyInformation
        })
    }

    //发异步ajax请求
    componentDidMount() {
        this.getCompanyInformation()
    }

    //为第一次render()准备数据
    componentWillMount() {
        this.initColumns()
    }

    //初始化Table所有列的数组
    initColumns = () => {
        this.columns = [
            {
                title: '公司名称',
                dataIndex: 'name',//显示数据对应的属性名
                width: "10%",
            },
            {
                title: '公司介绍',
                dataIndex: 'describe',
                width: "20%",
            },
            {
                title: '公司地址',
                dataIndex: 'address',
                width: "10%",
            },
            {
                title: 'LOGO图片',
                dataIndex: 'logo',
                render: (logo) => (
                    <span>
                        <img src={logo} alt="LOGO图片"/>
                    </span>
                )
            },
            {
                title: '营业执照',
                dataIndex: 'license',
                render: (license) => (
                    <span>
                        <img src={license} alt="营业执照"/>
                    </span>
                )
            },
            {
                title: '联系人',
                dataIndex: 'contact',
                width: "5%",
            },
            {
                title: '联系电话',
                dataIndex: 'contact_phone',
                width: "5%",
            },
            {
                title: '积分',
                dataIndex: 'points',
                width: "5%",
            },
            {
                title: '信用分',
                dataIndex: 'credit',
                width: "5%",
            },
            {
                title: '操作',
                width: 300,
                render: () => (

                    <span>
                        <Button type='primary' onClick={this.updateCom_information}>
                        <Icon type='form'/>
                            修改公司信息
                        </Button>
                    </span>
                )
            }
        ]
    }

    updateCom_information = () => {
        this.props.history.replace('/update_com_information')
    }


    render() {
        //读取状态数据
        const {companyInformation} = this.state
        return (
            <div>
                <Card>
                    <Table
                        rowKey='id'
                        dataSource={companyInformation}
                        columns={this.columns}/>
                </Card>
            </div>
        )
    }
}

